package cn.edu.ncut.cs.springboot.petmanagementsystem.service.Impl;

import cn.edu.ncut.cs.springboot.petmanagementsystem.common.PageResult;
import cn.edu.ncut.cs.springboot.petmanagementsystem.dto.ProductReviewRequest;
import cn.edu.ncut.cs.springboot.petmanagementsystem.dto.ServiceReviewRequest;
import cn.edu.ncut.cs.springboot.petmanagementsystem.mapper.ProductReviewMapper;
import cn.edu.ncut.cs.springboot.petmanagementsystem.mapper.ServiceReviewMapper;
import cn.edu.ncut.cs.springboot.petmanagementsystem.pojo.ProductReview;
import cn.edu.ncut.cs.springboot.petmanagementsystem.pojo.ServiceReview;
import cn.edu.ncut.cs.springboot.petmanagementsystem.service.ReviewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 评价服务实现类
 */
@Slf4j
@Service
public class ReviewServiceImpl implements ReviewService {

    @Autowired
    private ProductReviewMapper productReviewMapper;

    @Autowired
    private ServiceReviewMapper serviceReviewMapper;

    /**
     * 创建商品评价
     */
    @Override
    @Transactional
    public ProductReview createProductReview(Long userId, String username, ProductReviewRequest request) {
        log.info("创建商品评价，用户ID：{}，商品ID：{}，订单ID：{}", userId, request.getProductId(), request.getOrderId());

        // 检查是否已评价
        ProductReview existingReview = productReviewMapper.checkUserReview(
                userId, request.getOrderId(), request.getProductId());
        if (existingReview != null) {
            throw new RuntimeException("您已评价过该商品，无法重复评价");
        }

        // 创建评价记录
        ProductReview review = new ProductReview();
        BeanUtils.copyProperties(request, review);
        review.setUserId(userId);
        review.setUsername(username);

        // 处理匿名
        if (request.getIsAnonymous() != null && request.getIsAnonymous() == 1) {
            review.setUsername("匿名用户");
        }

        // 默认非匿名
        if (review.getIsAnonymous() == null) {
            review.setIsAnonymous(0);
        }

        productReviewMapper.insert(review);
        log.info("商品评价创建成功，评价ID：{}", review.getId());

        return review;
    }

    /**
     * 获取商品评价列表
     */
    @Override
    public PageResult<ProductReview> getProductReviews(Long productId, Integer page, Integer pageSize) {
        log.info("获取商品评价列表，商品ID：{}，页码：{}，每页大小：{}", productId, page, pageSize);

        // 参数默认值
        if (page == null || page < 1) {
            page = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }

        // 计算偏移量
        int offset = (page - 1) * pageSize;

        // 查询评价列表
        List<ProductReview> reviews = productReviewMapper.selectByProductId(productId, offset, pageSize);

        // 查询总数
        Integer total = productReviewMapper.countByProductId(productId);
        if (total == null) {
            total = 0;
        }

        // 使用PageResult的静态方法创建分页结果
        return PageResult.success(reviews, total.longValue(), (long) page, (long) pageSize);
    }

    /**
     * 创建服务评价
     */
    @Override
    @Transactional
    public ServiceReview createServiceReview(Long userId, String username, ServiceReviewRequest request) {
        log.info("创建服务评价，用户ID：{}，服务类型ID：{}，预约ID：{}", 
                userId, request.getServiceTypeId(), request.getReservationId());

        // 检查是否已评价
        ServiceReview existingReview = serviceReviewMapper.checkUserReview(userId, request.getReservationId());
        if (existingReview != null) {
            throw new RuntimeException("您已评价过该服务，无法重复评价");
        }

        // 创建评价记录
        ServiceReview review = new ServiceReview();
        BeanUtils.copyProperties(request, review);
        review.setUserId(userId);
        review.setUsername(username);

        // 处理匿名
        if (request.getIsAnonymous() != null && request.getIsAnonymous() == 1) {
            review.setUsername("匿名用户");
        }

        // 默认非匿名
        if (review.getIsAnonymous() == null) {
            review.setIsAnonymous(0);
        }

        serviceReviewMapper.insert(review);
        log.info("服务评价创建成功，评价ID：{}", review.getId());

        return review;
    }

    /**
     * 获取服务评价列表
     */
    @Override
    public PageResult<ServiceReview> getServiceReviews(Long serviceTypeId, Integer page, Integer pageSize) {
        log.info("获取服务评价列表，服务类型ID：{}，页码：{}，每页大小：{}", serviceTypeId, page, pageSize);

        // 参数默认值
        if (page == null || page < 1) {
            page = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }

        // 计算偏移量
        int offset = (page - 1) * pageSize;

        // 查询评价列表
        List<ServiceReview> reviews = serviceReviewMapper.selectByServiceTypeId(serviceTypeId, offset, pageSize);

        // 查询总数
        Integer total = serviceReviewMapper.countByServiceTypeId(serviceTypeId);
        if (total == null) {
            total = 0;
        }

        // 使用PageResult的静态方法创建分页结果
        return PageResult.success(reviews, total.longValue(), (long) page, (long) pageSize);
    }
}
